define('views/areas', ['jquery'], function ($) {
    function Areas() {
        this.addressUrl = '/src/assets/data/henan-zhengzhou.json'
        // this.areasLyerGroup = L.layerGroup()
        this.areasLyerGroup = L.featureGroup()
    }

    Areas.prototype.Init = function () {
        var that = this
        this.areasLyerGroup.addTo(window.map)
        this.showTooltipLevel = 12
        this.showTooltip = (window.map.getZoom()>=this.showTooltipLevel)

        window.map.on('zoomend',function(event){
            var zoom = event.target.getZoom()
            if(zoom >= that.showTooltipLevel && !that.showTooltip){
                that.showTooltip = !that.showTooltip
                that.areasLyerGroup.eachLayer(function(layer){
                    layer.unbindTooltip()
                    layer.bindTooltip(layer.options.showAddress,{direction:'top',permanent:true,className:'areaTooltip'})
                })
            }else if(zoom < that.showTooltipLevel && that.showTooltip){
                that.showTooltip = !that.showTooltip
                that.areasLyerGroup.eachLayer(function(layer){
                    layer.unbindTooltip()
                    layer.bindTooltip(layer.options.showAddress,{direction:'top',permanent:false,className:'areaTooltip'})
                })
            }
        })
    }

    /**
     * 移除地图上的点
     */
    Areas.prototype.removePoints=function(){
        this.areasLyerGroup.clearLayers()
    }

    /**
     * 给地图上添加点
     * @param {Array} points 
     */
    Areas.prototype.addPoints = function (points) {
        try{
            var that = this
            var isMobile = window.isMobile()
            var markers = points.map(function(p) {
                var popup = L.popup().setContent('曾确诊地点：'+p.showAddress+'<br/>曾确诊人数：' + (p.cntSumCertain>=0?(p.cntSumCertain+'&nbsp;人'):'暂无上报信息'))
                var className = p.cntSumCertain>=0?'areaMarker':'noAreaMarker'
                var icon = L.divIcon({ className: className, html: "<div>"+(p.cntSumCertain>=0?p.cntSumCertain:'-')+"</div>" })

                var marker = L.marker(L.latLng(Number(p.lat), Number(p.lng)), { icon: icon,showAddress:p.showAddress,cntSumCertain:p.cntSumCertain }).bindPopup(popup)
                if(!isMobile){
                    var permanent = window.map.getZoom()>=that.showTooltipLevel
                    marker.bindTooltip(p.showAddress,{direction:'top',permanent:permanent,className:'areaTooltip'})
                }
                return marker
            })
            
            markers.forEach(function(element) {
                that.areasLyerGroup.addLayer(element)
            });
        }catch(error){
            console.error(error)
        }
// fullAddress: "河南省郑州市郑东新区卢浮公馆"
// cntSumCertain: "1"
// lng: 113.76751
// lat: 34.738926
// showAddress
    }

    return Areas

});